<template>
    <div class="login-container">
    <div class="login-box">
      <!-- 标题区域 -->
      <div class="login-header">
        <h1 class="login-title">欢迎登录</h1>
        <p class="login-subtitle">请输入您的账号和密码</p>
      </div>

      <!-- 登录表单 -->
       <!-- ref相当于给组件起了个别名，方便后续将组件对象 fromEl 传入 submitForm()方法，进行validate()方法调用，校验提交-->
      <el-form
        ref="loginFormRef" :model="loginForm" class="login-form" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" size="large"/>
        </el-form-item>
        <el-form-item prop="password">
          <!-- 因为keydown这种事件只能在焦点在自身的情况下触发，所以绑定在登录按钮上不好使，需要绑定在输入框中 -->
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" size="large" @keydown.enter="submitForm(loginFormRef)"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="large" class="login-btn" @click="submitForm(loginFormRef)" >登录 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { ref, reactive } from 'vue'
//引入路由和小仓库
import { useRouter } from 'vue-router'
const $router = useRouter();
import useUserStore from '@/store/modules/user'
const useStore = useUserStore();
console.log(useStore.user);

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})
const loginFormRef = ref()

//校验规则
const validateUsername = (rule: any, value: any, callback: any) => {
  value = value.trim();
  const length = value && value.length;
  //正则表达式
  const reg = /^[a-zA-Z0-9_]+$/;
  if (value == '') {
    callback(new Error('请输入用户名'));
  } else if(length < 4 || length > 12){
    callback(new Error('长度在4到12位之间'));
  } else if(!reg.test(value)){
    callback(new Error('用户名只能是英文、数字和下划线组成'));
  } else {
    callback();
  }
}

//rules是表达式校验规则
const rules = reactive<FormRules>({
  username: [{ required:true,validator: validateUsername, trigger: ['blur','change'] }],
  password: [{ required:true,message:"请输入密码", trigger: 'blur' }],
  
})

//提交验证
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      //获取请求是否成功
      try {
        await useStore.userLogin(loginForm)
        //编程时路由跳转到首页
        $router.push("/");
        ElMessage({
          type:"success",
          message:"登录成功"
        })
      } catch (error:any) {
        ElMessage({
          type:"error",
          message: error.message
        })
      }
      
    } else {
      console.log('error submit!')
    }
  })
}
</script>

<style scoped>
body{
  height: 100%;
}
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 12px;
  padding: 40px 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
}

:deep(.el-form-item) {
  margin-bottom: 24px;
}

.login-btn {
  width: 100%;
  border-radius: 8px;
  height: 48px;
  font-size: 16px;
  margin-top: 10px;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
}

:deep(.el-link) {
  font-size: 14px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
    margin: 0 10px;
  }
  
  .login-title {
    font-size: 24px;
  }
}
</style>